﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace dzs.DAL
{
	/// <summary>
	/// 数据访问类:qualitysked2
	/// </summary>
	public partial class qualitysked2
	{
		public qualitysked2()
		{}
		#region  BasicMethod
        public bool setstate(string qidlist, string r)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update qualitysked2 set  qresult='" + r + "'");
            strSql.Append(" where qid in (" + qidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool setqList(string qidlist, int uid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update qualitysked2 set  uid=" + uid);
            strSql.Append(" where qid in (" + qidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("qid", "qualitysked2"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int qid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from qualitysked2");
			strSql.Append(" where qid=@qid");
			SqlParameter[] parameters = {
					new SqlParameter("@qid", SqlDbType.Int,4)
			};
			parameters[0].Value = qid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(dzs.Model.qualitysked2 model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into qualitysked2(");
			strSql.Append("spotunit,projectname,supplier,productname,ptype,winbatch,winnum,productdw,spottype,spotnum,spotcontent,projectunit,spotopinion,plandate,uid,qresult)");
			strSql.Append(" values (");
			strSql.Append("@spotunit,@projectname,@supplier,@productname,@ptype,@winbatch,@winnum,@productdw,@spottype,@spotnum,@spotcontent,@projectunit,@spotopinion,@plandate,@uid,@qresult)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@spotunit", SqlDbType.NVarChar,30),
					new SqlParameter("@projectname", SqlDbType.NVarChar,50),
					new SqlParameter("@supplier", SqlDbType.NVarChar,30),
					new SqlParameter("@productname", SqlDbType.NVarChar,30),
					new SqlParameter("@ptype", SqlDbType.NVarChar,15),
					new SqlParameter("@winbatch", SqlDbType.NVarChar,10),
					new SqlParameter("@winnum", SqlDbType.Float,8),
					new SqlParameter("@productdw", SqlDbType.NVarChar,10),
					new SqlParameter("@spottype", SqlDbType.NVarChar,10),
					new SqlParameter("@spotnum", SqlDbType.Float,8),
					new SqlParameter("@spotcontent", SqlDbType.NVarChar,200),
					new SqlParameter("@projectunit", SqlDbType.NVarChar,50),
					new SqlParameter("@spotopinion", SqlDbType.NVarChar,50),
					new SqlParameter("@plandate", SqlDbType.DateTime),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@qresult", SqlDbType.NVarChar,10)};
			parameters[0].Value = model.spotunit;
			parameters[1].Value = model.projectname;
			parameters[2].Value = model.supplier;
			parameters[3].Value = model.productname;
			parameters[4].Value = model.ptype;
			parameters[5].Value = model.winbatch;
			parameters[6].Value = model.winnum;
			parameters[7].Value = model.productdw;
			parameters[8].Value = model.spottype;
			parameters[9].Value = model.spotnum;
			parameters[10].Value = model.spotcontent;
			parameters[11].Value = model.projectunit;
			parameters[12].Value = model.spotopinion;
			parameters[13].Value = model.plandate;
			parameters[14].Value = model.uid;
			parameters[15].Value = model.qresult;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(dzs.Model.qualitysked2 model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update qualitysked2 set ");
			strSql.Append("spotunit=@spotunit,");
			strSql.Append("projectname=@projectname,");
			strSql.Append("supplier=@supplier,");
			strSql.Append("productname=@productname,");
			strSql.Append("ptype=@ptype,");
			strSql.Append("winbatch=@winbatch,");
			strSql.Append("winnum=@winnum,");
			strSql.Append("productdw=@productdw,");
			strSql.Append("spottype=@spottype,");
			strSql.Append("spotnum=@spotnum,");
			strSql.Append("spotcontent=@spotcontent,");
			strSql.Append("projectunit=@projectunit,");
			strSql.Append("spotopinion=@spotopinion,");
			strSql.Append("plandate=@plandate,");
			strSql.Append("uid=@uid,");
			strSql.Append("qresult=@qresult");
			strSql.Append(" where qid=@qid");
			SqlParameter[] parameters = {
					new SqlParameter("@spotunit", SqlDbType.NVarChar,30),
					new SqlParameter("@projectname", SqlDbType.NVarChar,50),
					new SqlParameter("@supplier", SqlDbType.NVarChar,30),
					new SqlParameter("@productname", SqlDbType.NVarChar,30),
					new SqlParameter("@ptype", SqlDbType.NVarChar,15),
					new SqlParameter("@winbatch", SqlDbType.NVarChar,10),
					new SqlParameter("@winnum", SqlDbType.Float,8),
					new SqlParameter("@productdw", SqlDbType.NVarChar,10),
					new SqlParameter("@spottype", SqlDbType.NVarChar,10),
					new SqlParameter("@spotnum", SqlDbType.Float,8),
					new SqlParameter("@spotcontent", SqlDbType.NVarChar,200),
					new SqlParameter("@projectunit", SqlDbType.NVarChar,50),
					new SqlParameter("@spotopinion", SqlDbType.NVarChar,50),
					new SqlParameter("@plandate", SqlDbType.DateTime),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@qresult", SqlDbType.NVarChar,10),
					new SqlParameter("@qid", SqlDbType.Int,4)};
			parameters[0].Value = model.spotunit;
			parameters[1].Value = model.projectname;
			parameters[2].Value = model.supplier;
			parameters[3].Value = model.productname;
			parameters[4].Value = model.ptype;
			parameters[5].Value = model.winbatch;
			parameters[6].Value = model.winnum;
			parameters[7].Value = model.productdw;
			parameters[8].Value = model.spottype;
			parameters[9].Value = model.spotnum;
			parameters[10].Value = model.spotcontent;
			parameters[11].Value = model.projectunit;
			parameters[12].Value = model.spotopinion;
			parameters[13].Value = model.plandate;
			parameters[14].Value = model.uid;
			parameters[15].Value = model.qresult;
			parameters[16].Value = model.qid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int qid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from qualitysked2 ");
			strSql.Append(" where qid=@qid");
			SqlParameter[] parameters = {
					new SqlParameter("@qid", SqlDbType.Int,4)
			};
			parameters[0].Value = qid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string qidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from qualitysked2 ");
			strSql.Append(" where qid in ("+qidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public dzs.Model.qualitysked2 GetModel(int qid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 qid,spotunit,projectname,supplier,productname,ptype,winbatch,winnum,productdw,spottype,spotnum,spotcontent,projectunit,spotopinion,plandate,uid,qresult from qualitysked2 ");
			strSql.Append(" where qid=@qid");
			SqlParameter[] parameters = {
					new SqlParameter("@qid", SqlDbType.Int,4)
			};
			parameters[0].Value = qid;

			dzs.Model.qualitysked2 model=new dzs.Model.qualitysked2();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public dzs.Model.qualitysked2 DataRowToModel(DataRow row)
		{
			dzs.Model.qualitysked2 model=new dzs.Model.qualitysked2();
			if (row != null)
			{
				if(row["qid"]!=null && row["qid"].ToString()!="")
				{
					model.qid=int.Parse(row["qid"].ToString());
				}
				if(row["spotunit"]!=null)
				{
					model.spotunit=row["spotunit"].ToString();
				}
				if(row["projectname"]!=null)
				{
					model.projectname=row["projectname"].ToString();
				}
				if(row["supplier"]!=null)
				{
					model.supplier=row["supplier"].ToString();
				}
				if(row["productname"]!=null)
				{
					model.productname=row["productname"].ToString();
				}
				if(row["ptype"]!=null)
				{
					model.ptype=row["ptype"].ToString();
				}
				if(row["winbatch"]!=null)
				{
					model.winbatch=row["winbatch"].ToString();
				}
				if(row["winnum"]!=null && row["winnum"].ToString()!="")
				{
					model.winnum=decimal.Parse(row["winnum"].ToString());
				}
				if(row["productdw"]!=null)
				{
					model.productdw=row["productdw"].ToString();
				}
				if(row["spottype"]!=null)
				{
					model.spottype=row["spottype"].ToString();
				}
				if(row["spotnum"]!=null && row["spotnum"].ToString()!="")
				{
					model.spotnum=decimal.Parse(row["spotnum"].ToString());
				}
				if(row["spotcontent"]!=null)
				{
					model.spotcontent=row["spotcontent"].ToString();
				}
				if(row["projectunit"]!=null)
				{
					model.projectunit=row["projectunit"].ToString();
				}
				if(row["spotopinion"]!=null)
				{
					model.spotopinion=row["spotopinion"].ToString();
				}
				if(row["plandate"]!=null && row["plandate"].ToString()!="")
				{
					model.plandate=DateTime.Parse(row["plandate"].ToString());
				}
				if(row["uid"]!=null && row["uid"].ToString()!="")
				{
					model.uid=int.Parse(row["uid"].ToString());
				}
				if(row["qresult"]!=null)
				{
					model.qresult=row["qresult"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select qid,spotunit,projectname,supplier,productname,ptype,winbatch,winnum,productdw,spottype,spotnum,spotcontent,projectunit,spotopinion,plandate,user_name,qresult ");
			strSql.Append(" FROM vqsked2 where 1=1");
			if(strWhere.Trim()!="")
			{
				strSql.Append(strWhere);
			}
            return DbHelperSQL.Query(strSql.ToString() + " order by qid desc");
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" qid,spotunit,projectname,supplier,productname,ptype,winbatch,winnum,productdw,spottype,spotnum,spotcontent,projectunit,spotopinion,plandate,uid,qresult ");
			strSql.Append(" FROM qualitysked2 ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM qualitysked2 ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.qid desc");
			}
			strSql.Append(")AS Row, T.*  from qualitysked2 T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "qualitysked2";
			parameters[1].Value = "qid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

